﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Fares
{
    public partial class PlatiSearchFrm : FrmRezumat
    {

        public String Filtru = String.Empty;
        public Boolean bCauta = true;
        public Boolean parteneriProprii = false;
        public PlatiSearchFrm(ModuleClass mc, Boolean _partprop)
            : base(mc)
        {
            parteneriProprii = _partprop;
            InitializeComponent();
            workConnString = mc.workConnectionString;
            EnableMainBrowseButton = false;
            documentePlataTableAdapter.SetTableName(mc.AnArhiva);
        }

        private void DocumenteSearchFrm_Load(object sender, EventArgs e)
        {         
            SqlConnection con = DeschideConexiune(workConnString);
            this.fares_ParteneriCUITableAdapter.Connection = con;
            if (!parteneriProprii)
                this.fares_ParteneriCUITableAdapter.Fill(this.esrpDataSetPlati.fares_ParteneriCUI);
            else
                this.fares_ParteneriCUITableAdapter.FillByUnitate(this.esrpDataSetPlati.fares_ParteneriCUI, ModuleInfo.CodUnitate);
            InchideConexiune(con);
            AddButton("Filtreaza[F9]", global::ERPStudio.Properties.Resources._001_371, Cauta, ToolStripItemAlignment.Right, false);
            //AddButton("Filtreaza", global::ERPStudio.Properties.Resources._001_38, Filtreaza, ToolStripItemAlignment.Right, false);
            HandleSalvare += Cauta;
        }

        private void addPartenerB_Click(object sender, EventArgs e)
        {
            if (partenerCB.SelectedIndex == -1)
            {
                MessageBox.Show("Va rugam selectati un partener existent!",
                   "Atentie",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Hand);
                return;
            }
            furnizoriArticoleSelectateBindingSource.Add(new FurnizoriArticoleSelectate(partenerCB.Text,
                Convert.ToInt64(partenerCB.SelectedValue)));
            parteneriRB.Checked = true;
        }

        private void delPart_Click(object sender, EventArgs e)
        {
            if (furnizoriArticoleSelectateBindingSource.Count > 0)
                furnizoriArticoleSelectateBindingSource.RemoveCurrent();
        }

        private void deLaDataDTP_Enter(object sender, EventArgs e)
        {
            dateRB.Checked = true;
        }

        private void laDataDTP_Enter(object sender, EventArgs e)
        {
            dataRB.Checked = true;
        }

        private void partenerCB_Enter(object sender, EventArgs e)
        {
            partenerRB.Checked = true;
        }

        new private void Cauta(object sender, EventArgs e)
        {

            Filtru = " CodUnitate=" + ModuleInfo.CodUnitate.ToString();

            if ((incasariCB.Checked && !platiCB.Checked) || (!incasariCB.Checked && platiCB.Checked))
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru = "TipOperatiune = '" + (incasariCB.Checked?"O":"I") + "'";
            }

            if (dataRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru = "Data='" + laDataDTP.Value.Year.ToString() + "-" + laDataDTP.Value.Month.ToString() + "-" +
                    laDataDTP.Value.Day.ToString() + "'";
            }
            if (dateRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";

                Filtru = "Data>='" + deLaDataDTP.Value.Year.ToString() + "-" + deLaDataDTP.Value.Month.ToString() + "-" +
                    deLaDataDTP.Value.Day.ToString() + "' and Data<='" + panaLaDataDTP.Value.Year.ToString() + "-" + panaLaDataDTP.Value.Month.ToString() + "-" +
                    panaLaDataDTP.Value.Day.ToString() + "'";
            }

            if (partenerRB.Checked)
            {
                if (partenerCB.SelectedIndex == -1)
                {
                    MessageBox.Show("Va rugam selectati un partener existent!",
                       "Atentie",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Hand);
                    return;
                }
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                
                Filtru += " IDPartener=" + partenerCB.SelectedValue.ToString();
            }

            if (parteneriRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDPartener in (";
                String filt = String.Empty;
                for (Int16 i = 0; i < furnizoriArticoleSelectateBindingSource.Count; i++)
                {
                    if (filt != String.Empty)
                        filt += ",";
                    filt += ((FurnizoriArticoleSelectate)furnizoriArticoleSelectateBindingSource.List[i]).Tag.ToString();
                }
                Filtru += filt + ")";
            }

            if (numarulTB.Text != String.Empty)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " Numar like '" + numarulTB.Text +"'";
            }
            
            documentePlataTableAdapter.Connection = DeschideConexiune(workConnString);
            documentePlataTableAdapter.FillByFiltru(esrpDataSetPlati.DocumentePlata, Filtru,ModuleInfo.AnArhiva);
            InchideConexiune(documentePlataTableAdapter.Connection);
            if (documenteBS.Count == 0)
            {
                MessageBox.Show("Nu s-a gasit nici o inregistrare care sa satisfaca criteriile selectate!",
                       "Atentie",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Hand);
                return;
            }
            this.DialogResult = DialogResult.OK;
            Close();

        }

        private void Filtreaza(object sender, EventArgs e)
        {
            bCauta = false;
            Cauta(sender,e);
        }

        private void dateRB_CheckedChanged(object sender, EventArgs e)
        {
            if (dateRB.Checked)
                dataRB.Checked = false;
        }

        private void dataRB_CheckedChanged(object sender, EventArgs e)
        {
            if (dataRB.Checked)
                dateRB.Checked = false;
        }

        private void partenerRB_CheckedChanged(object sender, EventArgs e)
        {
            if (partenerRB.Checked)
                parteneriRB.Checked = false;

        }

        private void parteneriRB_CheckedChanged(object sender, EventArgs e)
        {
            if (parteneriRB.Checked)
                partenerRB.Checked = false;

        }


    }
    
}
